package com.yjf.ljr.activity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.activity.IjActivity.ID;
import com.yjf.ljr.adapter.CommonAdapter;
import com.yjf.ljr.adapter.CommonAdapter.HandleCallBack;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.dataclass.PaymentDetailsDateClass;
import com.yjf.ljr.dataclass.PaymentDetailsDateClass.PaymentDetail;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.view.XListView;
import com.yjf.ljr.view.XListView.IXListViewListener;

public class PaymentDetailsActivity extends IjActivity implements OnClickListener {

	@ID("xlvpaymentdetails")
	private XListView mXlvpaymentdetails;
	@ID("llpaymentdetail_faile")
	private LinearLayout mLlllpaymentdetail_faile;
	/*
	 * @ID("llpaymentdetail") private LinearLayout mLlllpaymentdetail;
	 */
	/*
	 * @ID("current_outcome") private TextView mTvCurrentOutcome; // 资产总额
	 * 
	 * @ID("current_income") private TextView mTvCurrentIncome; // 资产总额
	 */
	private boolean mIsGettingData = false;
	private CommonAdapter mCommonAdapter;
	private List<PaymentDetail> mData = new ArrayList<PaymentDetail>();
	private boolean isOnLine = true;
	private int pageNo = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_payment_details);

		initControls();

		loadingData(pageNo, false, true);
	}

	/** 初始化控件 **/
	@SuppressLint("SimpleDateFormat")
	private void initControls() {
		setLeftBtnClick();
		setTitleStr("交易明细");

		mCommonAdapter = new CommonAdapter(this, mData, R.layout.item_payment_details, ViewHolder.class,
				mHandleCallBack);
		mXlvpaymentdetails.setAdapter(mCommonAdapter);
		mXlvpaymentdetails.setPullLoadEnable(true);
		mXlvpaymentdetails.setPullRefreshEnable(true);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mXlvpaymentdetails.setRefreshTime(sdf.format(new Date()));
		mXlvpaymentdetails.mFooterView.hide(); // 隐藏更多
		mXlvpaymentdetails.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				pageNo = 1;
				loadingData(pageNo, false, false);
			}

			@Override
			public void onLoadMore() {
				loadingData(++pageNo, true, false);
			}
		});
	}

	private void loadingData(int pageNo, boolean isAdd, boolean showProgress) {
		if (mIsGettingData) {
			return;
		}

		if (showProgress) {
			showProgressDialog();
		}
		new PaymentDetailTask(pageNo, isAdd).execute();
	}

	private void refresh() {

		loadingData(pageNo, false, true);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

		if (CommonData.FLAG_LOGIN_SERVER) {
			refresh(); // refresh account
			CommonData.FLAG_LOGIN_SERVER = false;
		} else {
			if (requestCode == CommonData.REQUEST_CODE_LOGIN && !isOnLine) {

				mLlllpaymentdetail_faile.setVisibility(View.VISIBLE);
				// mLlllpaymentdetail.setVisibility(View.GONE);
				mLlllpaymentdetail_faile.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						loadingData(pageNo, false, true);
						showProgressDialog();
					}
				});
				return;

			} else {
				mLlllpaymentdetail_faile.setVisibility(View.GONE);
				// mLlllpaymentdetail.setVisibility(View.GONE);
				return;

			}
		}
		super.onActivityResult(requestCode, resultCode, intent);

	}

	class PaymentDetailTask extends AsyncTask<Void, Void, String> implements OnClickListener {
		private int pageNo;
		private int pageSize = 20;
		private boolean isAdd = false;
		private PaymentDetailsDateClass dc = new PaymentDetailsDateClass();

		public PaymentDetailTask(int pageNo, boolean isAdd) {
			super();
			this.isAdd = isAdd;
			this.pageNo = pageNo;
		}

		@Override
		protected void onPreExecute() {

		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject reqObj = new RequestObject();
			reqObj.method = "incomOutcomQuery";
			reqObj.map.put("pageNumber"/*"pageNo"*/, pageNo);
			reqObj.map.put("pageSize", pageSize);
			return requestDataAndGetErrorMsg(reqObj, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			if ("-1".equals(dc.code)) {
				loginWebview();
				isOnLine = false;
				return;
			}
			if ("0".equals(dc.code)) {
				showToast(dc.msg);
				isOnLine = false;
				// showToast(CommonData.RESULT_SUCCESS);
//				mLlllpaymentdetail_faile.setVisibility(View.VISIBLE);
//				// mLlllpaymentdetail.setVisibility(View.GONE);
//				mLlllpaymentdetail_faile.setOnClickListener(new OnClickListener() {
//
//					@Override
//					public void onClick(View arg0) {
//						// TODO Auto-generated method stub
//						loadingData(pageNo, false, true);
//						showProgressDialog();
//					}
//				});

				dismissProgressDialog();
				return;
			}
			if (!TextUtils.isEmpty(result)) {
				isOnLine = false;
				mLlllpaymentdetail_faile.setVisibility(View.VISIBLE);
				// mLlllpaymentdetail.setVisibility(View.GONE);
				mLlllpaymentdetail_faile.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						loadingData(pageNo, false, true);
						showProgressDialog();
					}
				});
				showToast(result);
			} else {
				if (dc.totalCount == 0) {
					if (dc.queryList == null || dc.queryList.size() < 1) {
						showToast("暂无交易明细");
					} else {
						showToast(dc.msg);
					}
					mXlvpaymentdetails.stopRefresh();
					mXlvpaymentdetails.mFooterView.hide();
					mXlvpaymentdetails.stopLoadMore();
					mIsGettingData = false;
					mLlllpaymentdetail_faile.setVisibility(View.GONE);
					dismissProgressDialog();
					return;
				}
				/*
				 * if (TextUtils.isEmpty(dc.totalIncome)) {
				 * AppUtil.setViewHtml(mTvCurrentIncome,
				 * "<font color='#666666'>当月收入</font>" + 0 +
				 * "<font color='#666666'>元</font>"); } else {
				 * AppUtil.setViewHtml(mTvCurrentIncome,
				 * "<font color='#666666'>当月收入</font>" + dc.totalIncome +
				 * "<font color='#666666'>元</font>"); } if
				 * (TextUtils.isEmpty(dc.totalOutcome)) {
				 * AppUtil.setViewHtml(mTvCurrentOutcome,
				 * "<font color='#666666'>当月支出</font>" + 0 +
				 * "<font color='#666666'>元</font>"); } else {
				 * AppUtil.setViewHtml(mTvCurrentOutcome,
				 * "<font color='#666666'>当月支出</font>" + dc.totalOutcome +
				 * "<font color='#666666'>元</font>"); }
				 */
				isOnLine = true;
				mLlllpaymentdetail_faile.setVisibility(View.GONE);
				// mLlllpaymentdetail.setVisibility(View.VISIBLE);
				if (!isAdd) {
					mData.clear();
				}
				if (dc.queryList != null && dc.queryList.size() > 0) {
					mData.addAll(dc.queryList);
					mCommonAdapter.notifyDataSetChanged();

					if (mData.size() <= (dc.totalCount - 1) * dc.totalPage) {
						mXlvpaymentdetails.mFooterView.show();
					} else {
						mXlvpaymentdetails.mFooterView.hide();
					}
				}
			}
			mXlvpaymentdetails.stopRefresh();
			mXlvpaymentdetails.stopLoadMore();
			dismissProgressDialog();
			mIsGettingData = false;
		}

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.llpaymentdetail_faile:
				loadingData(pageNo, false, true);
				break;

			default:
				break;
			}
		}
	}

	public static class ViewHolder {
		TextView tvTime;
		TextView tvType;
		TextView tvMoneyacount;
		TextView current_outcome;
		TextView current_income;
		TextView tvInvestState;
		LinearLayout llItemlist;
	}

	private HandleCallBack mHandleCallBack = new HandleCallBack() {

		@Override
		public void handle(int position, View convertView, ViewGroup parent, Object holder, Object item) {
			PaymentDetail info = (PaymentDetail) item;
			ViewHolder hold = (ViewHolder) holder;
			// double temp_income = Double.parseDouble(info.inCome);
			// double temp_outcome = Double.parseDouble(info.outCome);

			// double temp_income = Double.parseDouble(info.inCome.replace(",",
			// ""));
			// double temp_outcome =
			// Double.parseDouble(info.outCome.replace(",", ""));

			if (!TextUtils.isEmpty(info.time) && info.time.length() > 10) {
				AppUtil.setViewText(hold.tvTime, info.time.substring(0, 10));
			} else {
				AppUtil.setViewText(hold.tvTime, info.time);
			}
			AppUtil.setViewText(hold.tvType, info.memo);
			AppUtil.setViewText(hold.tvInvestState, "交易成功");

			DecimalFormat df = new DecimalFormat(",##0.00");
			double temp_outcome = Double.parseDouble(info.outCome.replace(",", ""));
			if (0.0 == temp_outcome) {
				AppUtil.setViewText(hold.tvMoneyacount, info.inCome);
				hold.tvMoneyacount.setTextColor(Color.parseColor("#ff6600"));
			} else {
				AppUtil.setViewText(hold.tvMoneyacount, info.outCome);
				hold.tvMoneyacount.setTextColor(Color.parseColor("#1FA618"));

			}
			// AppUtil.setViewText(hold.tvMoneyacount, df.format(temp_income +
			// temp_outcome) + "");

			/*
			 * if ((temp_income + temp_outcome) > 0) {
			 * hold.tvMoneyacount.setTextColor(Color.parseColor("#1FA618")); }
			 * else {
			 * hold.tvMoneyacount.setTextColor(Color.parseColor("#ff6600")); }
			 */
			if (position % 2 == 0) {

				hold.llItemlist.setBackgroundColor(Color.parseColor("#FCEDDC"));
				/*
				 * hold.tvTime.setBackgroundColor(Color.parseColor("#FCEDDC"));
				 * 
				 * hold.tvType.setBackgroundColor(Color.parseColor("#FCEDDC"));
				 * hold
				 * .tvMoneyacount.setBackgroundColor(Color.parseColor("#FCEDDC"
				 * ));
				 */
			} else {

				hold.llItemlist.setBackgroundColor(Color.parseColor("#FFFFFF"));
				// hold.tvTime.setBackgroundColor(Color.parseColor("#FFFFFF"));
				// hold.tvType.setBackgroundColor(Color.parseColor("#FFFFFF"));
				// hold.tvMoneyacount.setBackgroundColor(Color.parseColor("#FFFFFF"));

			}
		}
	};

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.llpaymentdetail_faile:

			break;

		default:
			break;
		}
	}
}
